

# coding=utf-8

import numpy as np
import math

def direction_set(data):
    N = len(data)
    dir_set = {}
    v, h = np.where(data > 0)
    for i, j in zip(v, h):
        key = (str(i),str(j))
        if (j < 11) and (data[i, j + 1] > 0):
            dir_set[key] = [(str(i),str(j + 1))]
        if (i < 11) and (data[i + 1, j] > 0):
            if key in dir_set:
                dir_set[key] += [(str(i + 1),str(j))]
            else:
                dir_set[key] = [(str(i + 1),str(j))]
        # data[i, j-1]
        if (j > 0) and (data[i, j - 1]) > 0:
            if key in dir_set:
                dir_set[key] += [(str(i),str(j - 1))]
            else:
                dir_set[key] = [(str(i),str(j - 1))]
        # data[i-1, j]
        if (i > 0) and (data[i - 1, j] > 0):
            if key in dir_set:
                dir_set[key] += [(str(i - 1),str(j))]
            else:
                dir_set[key] = [(str(i - 1),str(j))]
    return dir_set


def get_forward_step(start_index, exit_index, direction):
    layer_ori = [start_index]
    while True:
        layer_sec = []
        for key in layer_ori:
            layer_sec += direction[key]
            if exit_index in direction[key]:
                forward_step = key
        if exit_index in layer_sec: break
        layer_ori = layer_sec
    return forward_step
#
#
# if __name__ == '__main__':
#     direction = direction_set(data)
#
#     huish = ['28']
#     # data[int(huish[0][0]), int(huish[0][1])] = 888
#     while True:
#         forward_step = get_forward_step(huish[-1])
#         huish += [forward_step]
#         if forward_step == '11':
#             break
#     step = huish[::-1][:-1]
#     for ind in step:
#         data[int(ind[0]), int(ind[1])] = -8
#     print(data)
#     print(step)



def player1Greedy(boardSet, player):

    for i in range(2):
       if boardSet[1][i][3] == player:
           x1 = boardSet[1][i][0]
           y1 = boardSet[1][i][1]
       else:
           x1_op = boardSet[1][i][0]
           y1_op = boardSet[1][i][0]

    N = math.sqrt(len(boardSet[0]))
    data = np.zeros((int(N), int(N)))
    Max = None
    Max_pos = None


    for k in boardSet[0]:
       if boardSet[0][k][0] == 4:
           data[k[0] - 1][k[1] - 1] = 1
       if boardSet[0][k][0] == 3:
           data[k[0] - 1][k[1] - 1] = 1
       elif boardSet[0][k][0] == 1:
           data[k[0] - 1][k[1] - 1] = 1
           if Max != None:
               Max_temp = boardSet[0][k][1] - (abs(k[0] - (x1 - 1)) + abs(k[1] - (y1 - 1)))*2 + (abs(k[0] - (x1_op - 1)) + abs(k[1] - (y1_op - 1)))*1
               if Max_temp > Max:
                   Max = Max_temp
                   Max_pos = (str(k[0]-1),str(k[1]-1))
           else:
               Max = boardSet[0][k][1] - (abs(k[0] - (x1 - 1)) + abs(k[1] - (y1 - 1)))*2 + (abs(k[0] - (x1_op - 1)) + abs(k[1] - (y1_op - 1)))*1
               Max_pos = (str(k[0]-1),str(k[1]-1))



    huish = [Max_pos]
    #print([(str(x1-1), str(y1-1)),huish[-1]])
    direction = direction_set(data)
    while True:
        forward_step = get_forward_step((str(x1-1), str(y1-1)),huish[-1], direction)
        huish += [forward_step]
        if forward_step == (str(int(x1)-1), str(int(y1)-1)):
                    break
    step = huish[::-1][:-1]




    if len(step) > 1:
        if int(step[1][0]) != int(step[0][0]):
            if int(step[1][0]) > int(step[0][0]):
                return 3
            else:
                return 2
        elif int(step[1][1]) != int(step[0][1]):
            if int(step[1][1]) > int(step[0][1]):
                return 0
            else:
                return 1
    else:
        if int(Max_pos[0]) != (int(x1) - 1):
            if int(Max_pos[0]) > (int(x1) - 1):
                return 3
            else:
                return 2
        elif int(Max_pos[1]) != (int(y1) - 1):
            if int(Max_pos[1]) > (int(y1) - 1):
                return 0
            else:
                return 1




def player2Greedy(boardSet, player):

    for i in range(2):
       if boardSet[1][i][3] == player:
           x1 = boardSet[1][i][0]
           y1 = boardSet[1][i][1]

    N = math.sqrt(len(boardSet[0]))
    data = np.zeros((int(N), int(N)))
    Max = None
    Max_pos = None

    for k in boardSet[0]:
       if boardSet[0][k][0] == 4:
           data[k[0] - 1][k[1] - 1] = 1
       if boardSet[0][k][0] == 3:
           data[k[0] - 1][k[1] - 1] = 1
       elif boardSet[0][k][0] == 1:
           data[k[0] - 1][k[1] - 1] = 1
           if Max != None:
               Max_temp = boardSet[0][k][1] - (abs(k[0] - (x1 - 1)) + abs(k[1] - (y1 - 1)))*2
               if Max_temp > Max:
                   Max = Max_temp
                   Max_pos = (str(k[0]-1),str(k[1]-1))
           else:
               Max = boardSet[0][k][1] - (abs(k[0] - (x1 - 1)) + abs(k[1] - (y1 - 1)))*2
               Max_pos = (str(k[0]-1),str(k[1]-1))
    print(Max)
    huish = [Max_pos]
    #print([(str(x1-1), str(y1-1)),huish[-1]])
    direction = direction_set(data)
    while True:
        forward_step = get_forward_step((str(x1-1), str(y1-1)),huish[-1], direction)
        huish += [forward_step]
        if forward_step == (str(int(x1)-1), str(int(y1)-1)):
                    break
    step = huish[::-1][:-1]

    print([(str(x1 - 1), str(y1 - 1)), Max_pos, step])


    if len(step) > 1:
        if int(step[1][0]) != int(step[0][0]):
            if int(step[1][0]) > int(step[0][0]):
                return 3
            else:
                return 2
        elif int(step[1][1]) != int(step[0][1]):
            if int(step[1][1]) > int(step[0][1]):
                return 0
            else:
                return 1
    else:
        if int(Max_pos[0]) != (int(x1) - 1):
            if int(Max_pos[0]) > (int(x1) - 1):
                return 3
            else:
                return 2
        elif int(Max_pos[1]) != (int(y1) - 1):
            if int(Max_pos[1]) > (int(y1) - 1):
                return 0
            else:
                return 1



